//
//  RBHomeToolView.m
//  Tourist
//
//  Created by Lan on 2017/7/15.
//  Copyright © 2017年 SummerTea. All rights reserved.
//

#import "RBNavigationBar.h"
#import "Masonry.h"
#import "UIImage+RBAdd.h"
#import "RBThemeConfig.h"

@interface RBNavigationBarGradientView: UIView

@property(nonatomic, strong) CALayer *gLayer;

@end

@implementation RBNavigationBarGradientView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        self.gLayer = gradientLayer;
        UIColor *startColor = [UIColor colorWithWhite:0 alpha:0.7];
        UIColor *endColor = [UIColor clearColor];
        gradientLayer.colors = @[(__bridge id)startColor.CGColor, (__bridge id)endColor.CGColor];
        gradientLayer.locations = @[@0.0, @1.0];
        gradientLayer.startPoint = CGPointMake(0.5, 0);
        gradientLayer.endPoint = CGPointMake(0.5, 1);
        gradientLayer.frame = self.bounds;
        [self.layer addSublayer:gradientLayer];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.gLayer.frame = self.bounds;
}

@end

@interface RBNavigationBar()

@property(nonatomic, strong) UIView *contentView;
@property(nonatomic, strong) RBNavigationBarGradientView *gradientView;

@end

@implementation RBNavigationBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
        
        self.contentView = [UIView new];
        self.contentView.backgroundColor = [RBThemeConfig themeColor];
        self.contentView.alpha = 0;
        [self addSubview:self.contentView];
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        
        self.gradientView = [RBNavigationBarGradientView new];
        [self addSubview:self.gradientView];
        [self.gradientView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        
        CGFloat statusBarH = [UIApplication sharedApplication].statusBarFrame.size.height;
        
        self.naviBar = [UIView new];
        [self addSubview:self.naviBar];
        [self.naviBar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsMake(statusBarH, 0, 0, 0));
        }];
        
        [self addObserver:self forKeyPath:@"backgroundAlpha" options:NSKeyValueObservingOptionNew context:NULL];
    }
    return self;
}

- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"backgroundAlpha"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"backgroundAlpha"] && self.needGradientView)
    {
        CGFloat value = [change[@"new"] floatValue];
        if (value <= 0.2)
        {
            self.gradientView.alpha = 1;
        }
        else
        {
            self.gradientView.alpha = MAX(0, (0.5-value)/0.3);
        }
    }
}

- (void)setBackgroundAlpha:(CGFloat)backgroundAlpha
{
    _backgroundAlpha = backgroundAlpha;
    self.contentView.alpha = backgroundAlpha;
}

- (void)setBgColor:(UIColor *)bgColor
{
    self.contentView.backgroundColor = bgColor;
}

- (void)setNeedGradientView:(BOOL)needGradientView
{
    _needGradientView = needGradientView;
    self.gradientView.hidden = !needGradientView;
}

@end

